/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年2月1日 下午2:01:25
 */
package com.lenovo.lmrp.server.assigntracer.repository;

import java.util.List;
import java.util.Objects;

import com.lenovo.lmrp.api.model.entity.EngineerPO;
import com.lenovo.lmrp.api.model.entity.EngineerTerminalPO;
import com.lenovo.lmrp.server.assigntracer.domain.engineer.EngineerBO;
import com.lenovo.lmrp.server.assigntracer.domain.engineer.IEngineerRepository;
import com.lenovo.sdc.framework.dao.ADaoFactory;
import com.lenovo.sdc.framework.dao.ICommonDAO;
import com.lenovo.sdc.framework.dao.RecordFilter;
import com.lenovo.sdc.framework.util.Condition;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * @author songyz
 *
 */
public class EngineerRepository implements IEngineerRepository {
    @Override
    public EngineerBO createEngineerBO(String itsmCode, String loginName) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            EngineerPO po = dao.get(EngineerPO.class, loginName);
            if (Objects.isNull(po))
                return null;
            else
                return new EngineerBO(itsmCode, po);
        }
        catch (Exception exp) {
            TraceableException.writeLog(ErrorCode.DB_QUERY_FAILED, itsmCode + ".engineerid:" + loginName, exp);
            return null;
        }
    }

    @Override
    public EngineerBO createEngineerWithTerminal(String itsmCode, String loginName) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            EngineerPO engineerPO = dao.get(EngineerPO.class, loginName);
            if (Objects.isNull(engineerPO))
                return null;

            // 查询工程师设备
            RecordFilter filter = new RecordFilter();
            filter.addCondition(new Condition<String, Object>("engineerid", loginName));
            List<EngineerTerminalPO> engineerTerminalList = dao.filteByCondition(EngineerTerminalPO.class, filter);
            engineerPO.setTerminals(engineerTerminalList);
            
            return new EngineerBO(itsmCode, engineerPO);
        }
        catch (Exception exp) {
            TraceableException.writeLog(ErrorCode.DB_QUERY_FAILED, itsmCode + ".engineerid:" + loginName, exp);
            return null;
        }
    }

    @Override
    public void store(String itsmCode, EngineerPO engineerPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            dao.beginTrans();
            dao.save(engineerPO);
            dao.endTrans();
        }
        catch (Exception exp) {
            TraceableException
                .writeLog(ErrorCode.DB_INSERT_FAILED, itsmCode + ".engineerid:" + engineerPO.getId(), exp);
        }
    }

    @Override
    public void store(String itsmCode, EngineerTerminalPO engineerTerminalPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            dao.beginTrans();
            dao.save(engineerTerminalPO);
            dao.endTrans();
        }
        catch (Exception exp) {
            TraceableException.writeLog(ErrorCode.DB_INSERT_FAILED,
                itsmCode + ".engineerid:" + engineerTerminalPO.getId(), exp);
        }
    }

    public void update(String itsmCode, EngineerTerminalPO engineerTerminalPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            dao.beginTrans();
            dao.update(engineerTerminalPO);
            dao.endTrans();
        }
        catch (Exception exp) {
            TraceableException.writeLog(ErrorCode.DB_UPDATE_FAILED,
                itsmCode + ".engineerid:" + engineerTerminalPO.getId(), exp);
        }
    }
}
